﻿using System;
using System.Diagnostics;
using System.Reflection;

namespace PerformanceDude.MSBuildShellExtension
{
    [DebuggerDisplay("{ConfigurationName}, {FullName}, {PlatformName}")]
    public class SolutionConfiguration
    {
        static readonly Type _ConfigurationInSolution;
        
        static readonly PropertyInfo _ConfigurationName;
        static readonly PropertyInfo _FullName;
        static readonly PropertyInfo _PlatformName;

        static SolutionConfiguration()
        {
            _ConfigurationInSolution = Type.GetType("Microsoft.Build.Construction.ConfigurationInSolution, Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", false, false);
            if (_ConfigurationInSolution != null)
            {
                _ConfigurationName = _ConfigurationInSolution.GetProperty("ConfigurationName", BindingFlags.NonPublic | BindingFlags.Instance);
                _FullName = _ConfigurationInSolution.GetProperty("FullName", BindingFlags.NonPublic | BindingFlags.Instance);
                _PlatformName = _ConfigurationInSolution.GetProperty("PlatformName", BindingFlags.NonPublic | BindingFlags.Instance);
            }
        }

        public string ConfigurationName { get; private set; }
        public string FullName { get; private set; }
        public string PlatformName { get; private set; }

        public SolutionConfiguration(object solutionProject)
        {
            this.ConfigurationName = _ConfigurationName.GetValue(solutionProject, null) as string;
            this.FullName = _FullName.GetValue(solutionProject, null) as string;
            this.PlatformName = _PlatformName.GetValue(solutionProject, null) as string;
        }
    }
}
